package util;

import java.util.Iterator;

/**
 * 
 * @author Alex
 *
 */
public class StringUtils {
	
	public static final String EMPTY = "";
	
    public static String join(Iterable<?> iterable, char separator) {
        return join(iterable, String.valueOf(separator));
    }
    
    public static String join(Iterable<?> iterable, String separator) {
        if (iterable == null) {
            return null;
        }
        return join(iterable.iterator(), separator);
    }
  
    public static String join(Iterator<?> iterator, String separator) {

        // handle null, zero and one elements before building a buffer
        if (iterator == null) {
            return null;
        }
        if (!iterator.hasNext()) {
            return EMPTY;
        }
        Object first = iterator.next();
        if (!iterator.hasNext()) {
            return first == null? "" : first.toString();
        }

        // two or more elements
        StringBuilder buf = new StringBuilder(256); // Java default is 16, probably too small
        if (first != null) {
            buf.append(first);
        }

        while (iterator.hasNext()) {
            if (separator != null) {
                buf.append(separator);
            }
            Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
        }
        return buf.toString();
    }
    
    public static boolean isNumeric(String str)
    {
      return str.matches("-?\\d+(.\\d+)?");
    }
}
